<?php

namespace app\manage\controller;


use app\common\validate\merchant\MerchantStatisticValid;
use app\common\validate\merchant\MerchantValid;
use app\manage\loader\ManageServices;
use think\Exception;

class System extends BaseApi
{

    /**
     * 系统操作日志
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function system_log()
    {
        try {
            $getData = input('get.');
            $option = $this->formatOption($getData);
            $stime = trim($getData['stime'] ?? '');
            $etime = trim($getData['etime'] ?? '');
            $where = [];
            if (!empty($stime) && !empty($etime)) {
                $where[] = ['create_at', 'between', [$stime . ' 00:00:00', $etime . ' 23:59:59']];
            } else {
                if (!empty($stime)) {
                    $where[] = ['create_at', '>=', $stime];
                }
                if (!empty($etime)) {
                    $where[] = ['create_at', '<=', $etime];
                }
            }
            $option['where'] = $where;
            $result = ManageServices::System()->systemLog($option);
            api_result($result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 首页统计
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function system_home()
    {
        $getData = input('get.');
        try {
            $result = ManageServices::System()->systemHome($getData['date'] ?? 'day');
            api_result(API_SUCCESS,'',$result);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 首页管理员统计
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function system_admin_home()
    {
        $getData = input('get.');
        try {
            $this->validate($getData, MerchantStatisticValid::class . '.statistic');
            $result = ManageServices::System()->systemAdminHome(
                $getData['date'] ?? 'day',
                $getData['statistic_date'] ?? '',
                $getData['page'] ?: 1,
                $getData['page_size'] ?: 15,
            );
            api_result(API_SUCCESS,'',$result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 首页商户统计
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function system_merchant_home()
    {
        $getData = input('get.');
        try {
            $this->validate($getData, MerchantStatisticValid::class . '.statistic');
            $result = ManageServices::System()->systemMerchantHome(
                $getData['date'],
                $getData['statistic_date'] ?? '',
                $getData['page'] ?: 1,
                $getData['page_size'] ?: 15,
                $getData['mer_id'] ?? '',
            );
            api_result(API_SUCCESS,'',$result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 金额明细
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function system_merchant_detail_home()
    {
        $getData = input('get.');
        try {
            $this->validate($getData, MerchantStatisticValid::class . '.statistic_data');
            $result = ManageServices::System()->systemMerchantDetailHome(
                $getData['mer_id'],
                $getData['date'],
                $getData['statistic_date'],
                $getData['page'] ?: 1,
                $getData['page_size'] ?: 15,
            );
            api_result(API_SUCCESS,'',$result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 导购扫码红包积分明细
     * @return void
     */
    public function system_manager_detail_home()
    {
        $getData = input('get.');
        try {
            $this->validate($getData, MerchantStatisticValid::class . '.statistic_data');
            $result = ManageServices::System()->systemManagerFundHome(
                $getData['mer_id'],
                $getData['date'],
                $getData['statistic_date'],
                $getData['page'] ?: 1,
                $getData['page_size'] ?: 15,
            );
            api_result(API_SUCCESS,'',$result);
        } catch (Exception $e) {
            api_result($e);
        }
    }


}
